package com.fg.pro.ai.AiController;

import com.fg.pro.model.reponse.ResponseResult;
import com.fg.pro.model.sys.entity.SysMenu;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.deepseek.DeepSeekAssistantMessage;
import org.springframework.ai.deepseek.DeepSeekChatModel;
import org.springframework.ai.deepseek.DeepSeekChatOptions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.List;

@Tag(name = "aiDeepSeek",description = "ai_deepSeek")
@RestController
@RequestMapping("aiDeepSeek")
public class AiDeepSeekController {

    @Resource
    DeepSeekChatModel deepSeekChatModel;

    @Operation(summary = "阻塞模式回答",description = "")
    @GetMapping("/chartByBlock")
    public ResponseResult chartByBlock(@RequestParam(required = false)String question){
        String call = deepSeekChatModel.call(question);
        return ResponseResult.okResult(call);
    }

    @Operation(summary = "流模式回答",description = "")
    @GetMapping("/chartByStream")
    public ResponseResult chartByStream(@RequestParam(required = false)String question){
        Flux<String> stream = deepSeekChatModel.stream(question);
        String call = new String();
        for (String strs : stream.toIterable()) {
            System.out.println(strs);
            call = call +strs;
        }
        return ResponseResult.okResult(call);
    }

    @Operation(summary = "调整配置项回答",description = "")
    @GetMapping("/chartByOptions")
    public ResponseResult chartByOptions(@RequestParam(required = false)String question){
        DeepSeekChatOptions options = DeepSeekChatOptions.builder()
                .model("deepseek-reasoner")
                .temperature(1.9d)
                .maxTokens(1024)
                .build();
        Prompt prompt = new Prompt(question, options);
        ChatResponse call = deepSeekChatModel.call(prompt);
        return ResponseResult.okResult(call.getResult().getOutput().getText());
    }

    @Operation(summary = "深度思考阻塞模式回答",description = "")
    @GetMapping("/chartByLockByDeep")
    public ResponseResult chartByDeep(@RequestParam(required = false)String question){
        Prompt prompt = new Prompt(question);
        ChatResponse response = deepSeekChatModel.call(prompt);
        DeepSeekAssistantMessage message = (DeepSeekAssistantMessage)response.getResult().getOutput();
        String reasoningContent = message.getReasoningContent();
        System.out.println("深度思考："+reasoningContent);
        return ResponseResult.okResult(message.getText());
    }

}
